﻿@section header {
    <style type="text/css">
        body
        {
            height: 100%;
            width: 100%;
        }
        .MBmenu li
        {
            background: url(/Content/images/book.png) no-repeat 2px 2px;
        }
    </style>
    <script type="text/javascript" src="/content/swfupload/swfupload.js"></script>
    <script type="text/javascript" src="/content/swfupload/MBupload.js"></script>
    <script type="text/javascript">
        Ext.form.HtmlEditor.test = Ext.extend(Ext.util.Observable, {
            init: function(cmp){
                this.cmp = cmp;
                this.cmp.on('render', this.onRender, this);
            },
            onRender: function(){
                var cmp = this.cmp;
                var btn = this.cmp.getToolbar().add({
                    iconCls: 'x-edit-createImg',
                    handler: function(){
                        this.cmp.insertAtCursor('333333333333333333');
                    },
                    scope: this,
                    tooltip: 'test'
                });
            }
        });

        Ext.form.HtmlEditor.Image = Ext.extend(Ext.util.Observable, {
	        langTitle: '插入图片',
            urlSizeVars: ['width','height'],
            basePath: '/',
            init: function(cmp){
                this.cmp = cmp;
                this.cmp.on('render', this.onRender, this);
                this.cmp.on('initialize', this.onInit, this, {single: true});
            },
            onEditorMouseUp : function(e){
                Ext.get(e.getTarget()).select('img').each(function(el){
                    var w = el.getAttribute('width'), h = el.getAttribute('height'), src = el.getAttribute('src')+' ';
                    src = src.replace(new RegExp(this.urlSizeVars[0]+'=[0-9]{1,5}([&| ])'), this.urlSizeVars[0]+'='+w+'$1');
                    src = src.replace(new RegExp(this.urlSizeVars[1]+'=[0-9]{1,5}([&| ])'), this.urlSizeVars[1]+'='+h+'$1');
                    el.set({src:src.replace(/\s+$/,"")});
                }, this);
        
            },
            onInit: function(){
                Ext.EventManager.on(this.cmp.getDoc(), {
			        'mouseup': this.onEditorMouseUp,
			        buffer: 100,
			        scope: this
		        });
            },
            onRender: function() {
                var btn = this.cmp.getToolbar().add({
                    iconCls: 'x-edit-createImg',
                    handler: this.selectImage,
                    scope: this,
                    tooltip: this.langTitle,
                    overflowText: this.langTitle
                });
            },
            selectImage: function(){
                var editor = this;
                var pos = this.cmp.getPosition();
                Ext.create('Ext.window.Window',{
                    title:'上传图片',
                    layout:'fit',
                    //modal: true,
                    width: 400,
                    border: 0,
                    items: [
                        {
                            xtype:'form',
                            bodyPadding: 6,
                            items:[{ 
                                xtype:'filefield',
                                id: 'imgFile',
                                labelWidth: 60,
                                fieldLabel: '选择图片',
                                anchor: '100%',
                                buttonText: '选择...',
                                value: ''
                            }],
                            buttons: [
                                {
                                    text: '上传', 
                                    handler: function(btn, e){
                                        var form = this.up('form').getForm();
                                        if(form.isValid()){
                                            form.submit({
                                                url: '/office/myOffice/imgSend',
                                                //waitMsg: 'Uploading your photo...',
                                                success: function(form, action) {
                                                    //debugger;
                                                    btn.up('window').close();
                                                    var img={Name:'img',src: action.result.fileName};
                                                    editor.insertImage(img);
                                                }

                                            });
                                        }
                                    }
                                },
                                {
                                    text: '取消',
                                    handler: function(){this.up('window').close();}
                                }
                            ]
                        }
                    ]
                }).show();
            },
            insertImage: function(img) { //有问题,没有插入到之前的光标位置
                //debugger;
                //editor.cmp.setActive(true);
                //editor.cmp.setPosition(pos);
                this.cmp.insertAtCursor('<img src="'+this.basePath+ img.src + '" title="'+img.Name+'" alt="'+img.Name+'">');
                //this.cmp.setActive(true);
                //this.cmp.insertAtCursor('333333333333333333');
            }
        });

        Ext.onReady(function () {
            var treeSelectUser = new Ext.tree.TreePanel({
                store: new Ext.data.TreeStore({
                    proxy: {
                        type: 'ajax',
                        url: '/data/Privilege/OrgUser'
                    },
                    root: {
                        text: '用户列表',
                        expanded: true
                    }
                }),
                stateful: true,
                rootVisible: false
            });

            var win = new Ext.window.Window({
                id: 'fwin',
                title: '选择用户',
                modal: true,
                closable: true,
                //stateful: true,
                width: 300,
                height: 500,
                bodyStyle: 'padding: 5px;',
                layout: 'fit',
                items: [
                                        treeSelectUser
                                    ],
                closeAction: 'hide',
                buttons: [{
                    text: '确定',
                    handler: function () {
                        var selectNodes = treeSelectUser.view.getChecked();
                        var users = '';
                        for (var i = 0; i < selectNodes.length; i++) {
                            users = (users.length > 0 ? users + ',' : '') + selectNodes[i].data.text + '(' + selectNodes[i].data.id + ')';
                        }
                        //alert(users);
                        Ext.getCmp('formNewMsg').getForm().findField('Receivers').setValue(users);
                        win.close();
                        //debugger;
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        win.close();
                    }
                }
                ]
            });

            Ext.create('Ext.container.Viewport', {
                id: 'Viewport',
                layout: 'fit',
                border: 0,
                items: [{
                    xtype: 'form',
                    id: 'formNewMsg',
                    layout: {
                        type: 'vbox',
                        align: 'stretch'
                    },
                    split: true,
                    autoScroll: true,
                    width: '100%',
                    //standardSubmit: true,
                    //bodyPadding: 20,
                    margin: 0,
                    tbar: [
                        { xtype: 'label', text: '我的消息中心 -> 新建消息', cls: 'formNav' },
                        { xtype: 'tbfill' },
                        { xtype: 'button', iconCls: 'helpIcon', tooltip: '帮助', tooltipType: 'title', handle: function (source, e) { alert('帮助!') } },
                        { xtype: 'button', iconCls: 'iconClose', text: '', tooltip: '关闭', tooltipType: 'title', handler: function (source, e) { window.parent.closeMe(window) } }
                        ],
                    defaults: { labelWidth: 80, layout: 'anchor', anchor: '100%', xtype: 'textfield' },
                    fieldDefaults: { labelAlign: 'left', msgTarget: 'none', margin: "1px 10px" },
                    items: [{
                        fieldLabel: '来源消息ID',
                        xtype: 'hiddenfield',
                        name: 'ParentID',
                        value: '@ViewBag.ParentID'
                    }, {
                        xtype: 'fieldcontainer',
                        //anchor: '100%',
                        layout: 'hbox',
                        fieldLabel: '收件人',
                        hidden: @ViewBag.hideReceiver,
                        items: [{
                            xtype: 'textareafield',
                            name: 'Receivers',
                            flex: 1,
                            height: 40,
                            value: '@ViewBag.Receiver'
                        }, {
                            xtype: 'splitter'
                        }, {
                            xtype: 'button',
                            text: '选择用户...',
                            height: 40,
                            handler: function () {
                                win.show();
                            }
                        }]
                    }, {
                        xtype: 'textfield',
                        fieldLabel: '消息标题',
                        name: 'Title',
                        value: '@ViewBag.Title'
                    }, {
                        fieldLabel: '消息正文',
                        xtype: 'htmleditor',
                        labelAlign: 'top',
                        fontFamilies: ['Tahoma','宋体','楷体','仿宋','黑体'],
                        createLinkText: '请输入URL:',
                        name: 'Content',
                        flex: 1,
                        plugins: [new Ext.form.HtmlEditor.test(), new Ext.form.HtmlEditor.Image()],
                        value: '@Html.Raw(@ViewBag.Content)' //直接写不行
                    },{
                        fieldLabel: '上传文件ID列表',
                        xtype: 'hiddenfield',
                        id: 'hiddenFileIDs',
                        name: 'hiddenFileIDs',
                        value: ''
                    },{
                        xtype: 'panel',
                        height: 50,
                        margin: '4px 10px',
                        autoScroll: true,
                        contentEl: 'fileUploadDiv'
                    }/*,{
                        xtype: 'filefield',
                        fieldLabel: '附件',
                        //buttonOnly: true,
                        buttonText: '选择...',
                        name: 'InfoFile',
                        listeners: {
                            change: function (FileUploadField, filename) {
                                var clone = new Ext.form.field.File(this.cloneConfig());
//                                this.setVisible(false);

//                                var filesList = Ext.getCmp('filesList')
//                                if (filesList == null) {
//                                    Ext.getCmp('formNewMsg').add({
//                                        id: 'filesList',
//                                        xtype: 'displayfield',
//                                        html: 'ddddd'
//                                    });
//                                }
//                                else {
//                                    filesList.setValue(filesList.getValue + 'sssssssss');
//                                }
                                this.up('form').add(clone);
                                this.up('form').doLayout();
                            }
                        }
                    }*/
                    ],
                    buttons: [
                        { text: '发送', handler: function () {
                            this.up('form').getForm().submit({
                                url: '/office/myOffice/MsgSend',
                                success: function (form, action) { alert('消息发送成功!'); self.parent.closeMe(window); },
                                failure: function (form, action) { alert('failure') }
                            }
                            );
                        }
                        },
                        { text: '取消', handler: function () { } }
                    ]
                }]
            });

            CreateUploadCtl('fileUploadDiv',Ext.getCmp('hiddenFileIDs').inputEl.id);
        });


    </script>
}
<div style="display: none;">
    <div id="fileUploadDiv">
    </div>
</div>
